package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/19.<br><br>
 *
 * 七进制数
 */
public class Topic504 {

    public static void main(String[] args) {
        Topic504 instance = new Topic504();

        System.out.println(instance.convertToBase7(100));
        System.out.println(instance.convertToBase7(7));
        System.out.println(instance.convertToBase7(-7));
        System.out.println(instance.convertToBase7(0));
    }


    public String convertToBase7(int num) {
        if(num == 0) {
            return "0";
        }

        StringBuilder builder = new StringBuilder();

        int temp = num > 0 ? num : -num;
        while (temp != 0) {
            builder.append(temp % 7);
            temp = temp / 7;
        }

        if(num < 0) {
            builder.append("-");
        }

        return builder.reverse().toString();
    }
}
